package com.ycz.sas.jgen.domain;


import java.util.Vector;

import javax.swing.table.AbstractTableModel;

public class TableModel extends AbstractTableModel {
	private Vector content = null;
	private String[] title_name;

	public TableModel() {
		content = new Vector();
	}
	
	public TableModel(String[] cols) {
		content = new Vector();
		title_name = cols;
	}

	public TableModel(int count) {
		content = new Vector(count);
	}

	public void addRow(String[] rowdata) {
		Vector v = new Vector(rowdata.length);
		for (int i = 0; i < rowdata.length; i++) {
			v.add(i, rowdata[i]);
		}
		content.add(v);
	}
	
	public boolean containRow(String name){
		for (int i = 0, len = content.size(); i < len; i++) {
			Vector v = (Vector) content.get(i);
			String value = (String) v.get(0);
			if(value.equals(name))
				return true;
		}
		return false;
	}

	public void removeRow(int row) {
		content.remove(row);
	}

	public void removeRows(int row, int count) {
		for (int i = 0; i < count; i++) {
			if (content.size() > row) {
				content.remove(row);
			}
		}
	}

	public int getColumnCount() {
		return title_name.length;
	}

	public int getRowCount() {
		return content.size();
	}

	public Object getValueAt(int row, int col) {
		return ((Vector) content.get(row)).get(col);
	}

	public String getColumnName(int col) {
		return title_name[col];
	}

	public void setValueAt(Object value, int row, int col) {
		((Vector) content.get(row)).remove(col);
		((Vector) content.get(row)).add(col, value);
		this.fireTableCellUpdated(row, col);
	}

	public boolean isCellEditable(int rowIndex, int columnIndex) {
		if (columnIndex == 0) {
			return false;
		}
		return true;
	}
}
